﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>HEW Javascript test</title>
    
    <!-- load the dojo toolkit base -->
	<script type="text/javascript" src="dojotoolkit/dojo/dojo.js" djConfig="parseOnLoad:true, isDebug:true"></script>
       
	<style type="text/css">
	/* our CSS can go here */    
	</style>    

    <script type="text/javascript">
        /* our JavaScript will go here */

        // a very common method of loading code onLoad
        var init = function() {
            console.log("I run after the page is ready.");
            dojo.require("dojox.atom.io.model");
        };
        
        // dojo startup
        dojo.addOnLoad(init);
    
        function createXMLHttpRequest() {
            try { return new XMLHttpRequest(); } catch (e) { }
            try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { }
            try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { }
            alert("XMLHttpRequest not supported");
            return null;
        }

        function get_feed(text) {
            var xmlDoc;
            if (window.DOMParser) {
                var parser = new DOMParser();
                xmlDoc = parser.parseFromString(text, "text/xml");
            }
            else // Internet Explorer
            {
                xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                xmlDoc.async = "false";
                xmlDoc.loadXML(text);
            }
            
            var feed = new dojox.atom.io.model.Feed();
            feed.buildFromDom(xmlDoc.documentElement);
            return feed;
        }

        function Advisor(serverName) {
            
            this.XMLHttpRequestNotification = createXMLHttpRequest();
            this.XMLHttpRequestNotification.serverName = serverName;
            this.XMLHttpRequestNotification.Cookie = 0;
            this.XMLHttpRequestNotification.OnNewFeed = null;
           
            this.XMLHttpRequestNotification.onreadystatechange = function() {
                if (this.readyState != 4) {
                    return;
                }

                var serverResponse = this.responseText;

                console.log(serverResponse);

                try {
                    var feed = get_feed(serverResponse);
                    if (feed.categories[0].term == "advice") {
                        this.Cookie = feed.id;
                        console.log("Cookie = " + this.Cookie);
                    }
                    else {
                        if (this.OnNewFeed != null)
                            this.OnNewFeed();
                    }
                }
                catch (e) {
                }

                this.open("GET", "http://" + this.serverName + "/advice/" + this.Cookie, true);
                this.send(null);
            }
            this.init = function() {
                this.XMLHttpRequestNotification.open("GET", "http://" + this.XMLHttpRequestNotification.serverName + "/advice", true);
                this.XMLHttpRequestNotification.send(null);
            }
        }

        function sendSubscription(serverName) {
            var myConnector = new Advisor(serverName);
            myConnector.init();
        }

        // post sample
        
        function sendPost() {

            var postBody = "lorem=ipsum&name=binny";

            myXMLHttpRequest = createXMLHttpRequest();
            myXMLHttpRequest.open("POST", "http://localhost/vars/", true);
            myXMLHttpRequest.onreadystatechange = function() {
            if (myXMLHttpRequest.readyState != 4) {
                    return;
                }
                var serverResponse = myXMLHttpRequest.responseText;
                console.log(serverResponse);            
            }
            myXMLHttpRequest.setRequestHeader("Content-length", postBody.length);
            myXMLHttpRequest.send(postBody);
        }

    </script>

    <style type="text/css">
    </style>

</head>
    <body>
        <p>
            <input id="ButtonAdvice" type="button" value="Send GET http://serverName/advice for a new subscribe 'cookie' (Long polling)" 
                onclick="sendSubscription('localhost')" 
                style="margin:5px 0px 5px 0px; width: 550px;"/>                
       </p>
       <p>
           <input id="ButtonSendPost" type="button" value="Send POST http://serverName/vars" 
                onclick="sendPost()" style="margin:5px 0px 5px 0px; width: 382px;"/>                
        </p>       
    </body>
</html>
